package com.winit.openapi.ums.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.CustomerContactService;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.CheckMainContactCommand;
import com.winit.ums.spi.commnad.CustomerQueryCommand;
import com.winit.ums.spi.commnad.IdCommand;
import com.winit.ums.spi.customer.command.AddCustomerContactCommand;
import com.winit.ums.spi.customer.command.UpdateCustomerContactCommand;
import com.winit.ums.spi.vo.CustomerInfoVO;
import com.winit.ums.spi.vo.customer.contacts.CustomerContactVo;

@Controller
@RequestMapping(value = "/customer/contact")
public class CustomerContactController extends BaseController {

    @Resource
    private CustomerContactService customerContactService;  
    
    @Resource
    private UmsCustomerService umsCustomerService;
        
    /**
     * 2.根据Id查询联系人
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/queryById")
    public String queryCustomerContactsById(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        IdCommand idCommand = JSON.parseObject(requestMsg.getData().toString(),IdCommand.class);
        idCommand.setCtx(CommandContext.getContext());
        
        CustomerContactVo contactVo = customerContactService.getCustomerContactById(idCommand);
        responseMsg.setData(contactVo);
        return SUCCESS;
    }
    
    /**
     * 3.根据Id更新联系人
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/modifyById")
    public String modifyCustomerContactsById(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        UpdateCustomerContactCommand command = JSON.parseObject(requestMsg.getData().toString(),UpdateCustomerContactCommand.class);
        command.setCtx(CommandContext.getContext());       
        customerContactService.updateCustomerContact(command);
        return SUCCESS;
    }
    
    /**
     * 4.添加联系人
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/addContact")
    public String addCustomerContactsById(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CommandContext ctx = CommandContext.getContext();
        String userName = ctx.getUsername();
        CustomerQueryCommand userCommand = new CustomerQueryCommand();
        userCommand.setEmail(userName);
        CustomerInfoVO userVo = umsCustomerService.queryUser(userCommand);
        if (null == userVo || null == userVo.getCompanyId()) {
            throw new ApiException(ErrorCode.USER_NOT_FOUND, userName);
        }
        AddCustomerContactCommand command = JSON.parseObject(requestMsg.getData().toString(),AddCustomerContactCommand.class);
        command.setCustomerId(userVo.getCompanyId());
        command.setCtx(CommandContext.getContext());       
        customerContactService.addCustomerContact(command);
        return SUCCESS;
    }
    
    
    /**
     * 5.根据Id删除联系人
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/deleteContact")
    public String deleteCustomerContactsById(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        IdCommand command = JSON.parseObject(requestMsg.getData().toString(),IdCommand.class);
        command.setCtx(CommandContext.getContext());       
        customerContactService.deleteCustomerContactById(command);
        return SUCCESS;
    }
    
    /**
     * 6.根据Id更新主要联系人
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/updateMainContact")
    public String updateCheckedMainContact(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {        
        CheckMainContactCommand command = JSON.parseObject(requestMsg.getData().toString(),CheckMainContactCommand.class);
        command.setCtx(CommandContext.getContext());       
        customerContactService.updateCheckedMainContact(command);
        return SUCCESS;
    }
    
    
    

   
    
}
